<template>
    <div>

    </div>
</template>
<script>
import axios from "axios";
const addData = (param) => {
    return axios.post("/api/question/answer/submit", { uid: window.localStorage.getItem('uid'), point: param});
};
export default {
    created() {
        let params = new URLSearchParams(location.search);
        var code = params.get('code')
        var param_point = params.get('state')
        window.localStorage.setItem('param_point',param_point)

        var _this = this;
        if(code){
        var data=axios(
            {
                url:"/api/home/user/openId",
                method:"post",
                data:{
                "uc":code
                },
                contentType:"text",
                processData: false,
                dataType:"text"
            }
            ).then(function (response){
                _this.openID = response.data.data.open_id
                window.localStorage.setItem("openID",_this.openID)
                _this.uid = response.data.data.uid;
                if(_this.uid!=0){//说明已经有信息了
                    window.localStorage.setItem("uid",_this.uid)
                    _this.submitpoint(param_point)
                }else{
                    _this.$router.push({name:'login',query:{fromURL:"submitquestion"}})
                }
            })
        }else{
            _this.submitpoint(param_point)
        }
    },
    methods: {
        submitpoint(val) {
            addData(val).then((data) => {
                if (data && data.data && data.data.meta.ecode === 0) {
                    window.localStorage.setItem('point',data.data.data.point)

                    this.$notify({ type: 'success', message: '提交成功' });
                    this.$router.push({name:'point',query:{point:data.data.data.point}});
                } else {
                    this.$notify({ type: 'warning', message: "新增提交失败" + ((data?.data?.meta?.emsg) ? ('：' + data?.data?.data?.emsg) : '') });
                }
            });

        }
    }
}
</script>